package com.gosophia.logContent.action;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.OrderCondition;
import com.gosophia.commons.Page;
import com.gosophia.commons.PropertyFilter;
import com.gosophia.commons.web.action.BaseAjaxAction;
import com.gosophia.logManagement.dto.LogContentDTO;
import com.gosophia.logManagement.ws.LogManagementWebService;

@Scope("prototype")
@Controller("OutLogContentAction")
public class OutLogContentAction extends BaseAjaxAction<LogContentDTO>{

	private static final long serialVersionUID = 4502952572064457231L;

    @Autowired
	private LogManagementWebService logManagementWebService;
    
    private Boolean searchFlg = false;
    
    // 下载文件时显示的文件名字
	private String fnames;

	@Override
	public LogContentDTO getDetailData() {
		return this.detailData;
	}

	@Override
	public Page<LogContentDTO> getPage() {
		return this.page;
	}

	public InputStream getInputStream() throws Exception {
		int count = logManagementWebService.findAllLogContents();
		
	    if(searchFlg.booleanValue() == false) {
	    	String  encode2 =  new  String(this.searchString.getBytes("ISO-8859-1"),"UTF-8");
	    	this.page = logManagementWebService.findSampleLogContentsByPage(1, encode2, count, orderConditions);
	    }else {
	    	if(propertyFilters.size()!=0&&propertyFilters!=null) {
	    	   for(PropertyFilter p1:propertyFilters) {
	    		   if("eventResource".equals(p1.getPropertyName()) || "eventUser.userName".equals(p1.getPropertyName())
	    			 || "eventUserAccount.userAccountName".equals(p1.getPropertyName()) || "eventAction".equals(p1.getPropertyName())) {
	    			   String  encode1 =  new  String(((String) p1.getPropertyValue()).getBytes("ISO-8859-1"),"UTF-8");
	    			   p1.setPropertyValue(encode1);
	    		   }
	    		   if("eventTime".equals(p1.getPropertyName())){
	    			   p1.setToCharFormat("yyyy-MM-dd hh24:mi:ss");
	    		   }
	    	   }
	    	}
	    	this.page = logManagementWebService.findLogContentsByPage(1, count, propertyFilters, orderConditions);
	    }
	    if(this.page.getTotalCount()<=0) {
	    	super.getRequestResult().setRsltCode("1");
	    	System.out.println("1");
	    	return null;
	    }else {
	    	super.getRequestResult().setRsltCode("0");
	    	super.getRequestResult().setMessage("数据已导出");
	    	logManagementWebService.outLogContent(this.page.getResult());
	    	FileInputStream in=new FileInputStream("c:/日志记录信息.xls");
			return  in;
	    }
		
	}

	@Override
	public void setOrderConditions(List<OrderCondition> orderConditions) {
		this.orderConditions = orderConditions;
	}

	@Override
	public void setPropertyFilters(List<PropertyFilter> propertyFilters) {
		this.propertyFilters = propertyFilters;
	}

//	@Override
//	public RequestResult getRequestResult() {
//		return requestResult;
//	}

//	@Override
//	public void setRequestResult(RequestResult requestResult) {
//		this.requestResult = requestResult;
//	}

	public Boolean getSearchFlg() {
		return searchFlg;
	}

	public void setSearchFlg(Boolean searchFlg) {
		this.searchFlg = searchFlg;
	}

	public String getFnames() throws UnsupportedEncodingException {
		fnames = "日志管理信息.xls";
		fnames = java.net.URLEncoder.encode(fnames, "UTF-8");// 处理中文文件名的问题
		fnames = new String(fnames.getBytes(), "ISO-8859-1"); 
		return fnames;
	}

	public void setFnames(String fnames) {
		this.fnames = fnames;
	}

	@Override
	public String _execute() throws Exception {
		// TODO Auto-generated method stub
		return SUCCESS;
	}

}
